//
//  BYScanCodeCompatible.swift
//  BYScan
//
//  Created by yetupal on 2023/6/29.
//

import UIKit

public protocol BYScanCodeCompatible {
    
    // 普通扫码  true 条形码 false 二维码
    func by_qrCode(modeValue: Bool, trigger: @escaping (String) -> Void)
    
    // 普通扫码  混合兼容模式
    func by_qrAllCode(trigger: @escaping (String) -> Void)
    
    // 进件扫码
    func by_mkCode(trigger: @escaping (String) -> Void)
    
    // 忽略识别
    func by_ignoreScan(ignoreValue: Int, trigger: @escaping (String) -> Void)
}

// MARK: - 扩展控制器
extension BYScanCodeCompatible where Self: UIViewController {
    
    // 普通扫码  混合兼容模式
    public func by_qrAllCode(trigger: @escaping (String) -> Void) {
        BYPermissions.by_authorizeCameraWith { [weak self] isValue in
            guard let wkThis = self else { return }
            // 没权限
            guard isValue else { wkThis.by_cameraAuthorityReminder(); return }
            let tempVC = BYAllScanViewController()
            tempVC.completionTrigger = trigger
            let navVC = UINavigationController(rootViewController: tempVC)
            navVC.modalPresentationStyle = .fullScreen
            wkThis.present(navVC, animated: true)
        }
    }
    
    /// 快捷绑定扫码
    public func by_qrCode(modeValue: Bool = false, trigger: @escaping (String) -> Void){
        
        BYPermissions.by_authorizeCameraWith { [weak self] isValue in
            guard let wkThis = self else { return }
            // 没权限
            guard isValue else { wkThis.by_cameraAuthorityReminder(); return }
            // 默认显示
            BYScanStyle.defaultServie.isWhetherBarCode = modeValue
            
            let tempVC = BYAkScanViewController()
            tempVC.completionTrigger = trigger
            let navVC = UINavigationController(rootViewController: tempVC)
            navVC.modalPresentationStyle = .fullScreen
            wkThis.present(navVC, animated: true)
        }
     
    }
    
    // 进件扫码
    public func by_mkCode(trigger: @escaping (String) -> Void)  {
        BYPermissions.by_authorizeCameraWith { [weak self] isValue in
            guard let wkThis = self else { return }
            // 没权限
            guard isValue else { wkThis.by_cameraAuthorityReminder(); return }

            let tempVC = BYAiScanViewController()
            tempVC.completionTrigger = trigger
            let navVC = UINavigationController(rootViewController: tempVC)
            navVC.modalPresentationStyle = .fullScreen
            wkThis.present(navVC, animated: true)
        }
    }

    // 忽略识别扫码 ignoreValue#扫码类型 0: 默认都识别 1: 只识别条形码 2: 只识别二维码
    public func by_ignoreScan(ignoreValue: Int, trigger: @escaping (String) -> Void)  {
        BYPermissions.by_authorizeCameraWith { [weak self] isValue in
            guard let wkThis = self else { return }
            // 没权限
            guard isValue else { wkThis.by_cameraAuthorityReminder(); return }

            let tempVC = BYAiScanViewController(ignoreValue: ignoreValue)
            tempVC.completionTrigger = trigger
            let navVC = UINavigationController(rootViewController: tempVC)
            navVC.modalPresentationStyle = .fullScreen
            wkThis.present(navVC, animated: true)
        }
    }
    
    
    private func by_cameraAuthorityReminder() -> Void {
        let currentAppName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String  ?? ""
        let alert = UIAlertController(title: "相机权限未开启", message: "请前往“设置-\(currentAppName)”中打开相机权限。", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "我知道了", style: .cancel))
        alert.addAction(UIAlertAction(title: "前往设置", style: .default, handler: { (sender) in
            BYPermissions.jumpToSystemPrivacySetting()
        }))
        self.present(alert, animated: true, completion: nil)
    }
}
